<?php
require_once dirname(__FILE__) . '/metadata.php';
require_once dirname(__FILE__) . '/EmailFetcher.class.php';
require_once dirname(__FILE__) . '/DbUtils.class.php';
require_once dirname(__FILE__) . '/includes/MiscUtils.class.php';

$emails = array();
$mailHeader = array();
$mailAttach = array();
$mailBody = '';
$mail = new EmailFetcher();
//$server_path = '/home/pozwebdev/hhcode.com/enqsys/attaches/';
//$connect = $mail->mailConnect('imap.webhost.co.nz', 993, 'enquiry@hhcode.com', 'weida911', 'INBOX', 'ssl');
$connect = $mail->mailConnect('imap.gmail.com', 993, 'dlenquiry@gmail.com', 'dlenquiry', 'INBOX', 'ssl');
$server_path = 'attaches/';
if($connect) {
  for ($mailId = 0, $totalCount = $mail->mailTotalCount(); $mailId < $totalCount; $mailId++) {
    $mailHeader = $mail->mailHeader($mailId + 1);
    $mailBody = $mail->getBody($mailId + 1);
    $mailAttach = $mail->getAttach($mailId + 1, $server_path);
    
    
    $myPdo = DbUtils::createPdoInst();
    if (!isset($emails[$mailHeader['from']])) {
      $cond_vals = new stdClass();
      $cond_vals->c = 't.email = :v1';
      $cond_vals->v = array(':v1' => $mailHeader['from']);
      $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      if (count($tmp) == 0) {
        $email = new stdClass();
        $email->track_id = MiscUtils::unique();
        $email->enquiry_id = 0;
        $email->email = $mailHeader['from'];
        $email->name = $mailHeader['fromName'];
        $email->date1 = date('Y-m-d H:i:s', strtotime($mailHeader['date']));
        $email->date2 = date('Y-m-d H:i:s', strtotime($mailHeader['date']));
        $email->unread = 1;
        DbUtils::add($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $email);
        $emails[$mailHeader['from']] = array($myPdo->lastInsertId(), 0, strtotime($mailHeader['date']));
        
      } else {
        $emails[$tmp[0]->email] = array($tmp[0]->id, $tmp[0]->enquiry_id, max(strtotime($mailHeader['date']), strtotime($tmp[0]->date1)));
      }
    } else {
      $emails[$mailHeader['from']][2] = max(strtotime($mailHeader['date']), $emails[$mailHeader['from']][2]);
    }

    $structure = imap_fetchstructure($connect, $mailId + 1);
    $message = MiscUtils::pluginObj();
    $message->date1 = date('Y-m-d H:i:s', strtotime($mailHeader['date']));
    $message->date2 = '0000-00-00 00:00:00';
    $message->date3 = '0000-00-00 00:00:00';
    $message->attr1 = $emails[$mailHeader['from']][0];
    $message->attr2 = $emails[$mailHeader['from']][1];
    $message->attr3 = $mailHeader['subject'];
    $message->attr4 = $mailBody;
    $message->attr5 = 0;
    $message->attr6 = $structure->type;
    $message->attr7 = 0;
    $message->attr8 = $mailHeader['from'];
    $message->attr9 = $mailHeader['fromName'];
    $message->message_type = 0;
    DbUtils::add($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $message);
    $message->id = $myPdo->lastInsertId();

    foreach ($mailAttach as $tmp) {
      $attach = new stdClass();
      $attach->track_id = MiscUtils::unique();
      $attach->enquiry_id = 0;
      $attach->email_id = $emails[$mailHeader['from']][0];
      $attach->message_id = $message->id;
      $attach->name_on_server = $tmp[0];
      $attach->name = $tmp[1];
      $attach->size = filesize($server_path . $tmp[0]) / 1000;
      DbUtils::add($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_message_attachments', $attach);
    }
    
    $mail->mailDelete($mailId + 1);
  }

  foreach ($emails as &$email) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.attr1 = :v1';
    $cond_vals->v = array(':v1' => $email[0]);
    $opts = new stdClass();
    $opts->select_expr = 'COUNT(t.id) AS count';
    $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;
    
    $obj = new stdClass();
    $obj->id = $email[0];
    $obj->date1 = date('Y-m-d H:i:s', $email[2]);
    $obj->date2 = date('Y-m-d H:i:s', $email[2]);
    $obj->unread = $tmp[0]->count;
    $tmp = DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $obj);
  }
  $mail->closeMail();
}
?>
